let html;
Page({
    data: {
        tempFilePaths: "",
        imgheight: 0,
        img: [],
        page_loading: !0,
    },

    onLoad: function(t) {
        var e = this;
        
        e.getIos()
    },
    getIos: function () {
        const platform = wx.getSystemInfoSync().platform
        const isIOS = platform === 'ios'
        this.setData({ isIOS})
        const that = this
        this.updatePosition(0)
        let keyboardHeight = 0
        wx.onKeyboardHeightChange(res => {
          if (res.height === keyboardHeight) return
          const duration = res.height > 0 ? res.duration * 1000 : 0
          keyboardHeight = res.height
          setTimeout(() => {
            wx.pageScrollTo({
              scrollTop: 0,
              success() {
                that.updatePosition(keyboardHeight)
                that.editorCtx.scrollIntoView()
              }
            })
          }, duration)
    
        })
    },
    updatePosition(keyboardHeight) {
        const toolbarHeight = 50
        const { windowHeight, platform } = wx.getSystemInfoSync()
        let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
        this.setData({ editorHeight, keyboardHeight })
    },
    setContent: function () {
        var that = this;
        getApp().core.getStorage({
            key: "goods_detail",
            success: function(t) {
                that.editorCtx.setContents({
                    html: t.data
                })
            }
        });
    },
    onEditorReady() {
        const that = this
        wx.createSelectorQuery().select('#editor').context(function (res) {
            that.editorCtx = res.context,
            that.setContent();
        }).exec()
    },
    blur() {
        this.editorCtx.blur()
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    onStatusChange(e) {
        const formats = e.detail
        this.setData({ formats })
    },
    format(e) {
        let { name, value } = e.target.dataset
        if (!name) return
        // console.log('format', name, value)
        this.editorCtx.format(name, value)
    },
    clear() {
        this.editorCtx.clear({
            success: function (res) {
            console.log("clear success")
            }
        })
    },
    removeFormat() {
        this.editorCtx.removeFormat()
        },
        insertDate() {
        const date = new Date()
        const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
        this.editorCtx.insertText({
            text: formatDate
        })
    },
    insertImage() {
        const that = this
        that.add();
        return ;
        wx.chooseImage({
            count: 1,
            success: function (res) {
                that.editorCtx.insertImage({
                    src: res.tempFilePaths[0],
                    data: {
                    id: 'abcd',
                    role: 'god'
                    },
                    width: '80%',
                    success: function () {
                    console.log('insert image success')
                    }
                })
            }
        })
    },
    
    add: function() {
        var that =this;
        getApp().core.chooseImage({
            count: 6,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                var a = e.tempFilePaths;
                a.forEach(function(e, o) {
                    getApp().core.showLoading({
                        type: "loading",
                        duration: 0
                    }), getApp().core.uploadFile({
                        url: getApp().api.default.upload_image,
                        filePath: e,
                        name: "image",
                        success: function(e) {
                            var n = JSON.parse(e.data)
                            that.editorCtx.insertImage({
                                src: n.data.url,
                                data: {
                                id: 'abcd',
                                role: 'god'
                                },
                                width: '100%',
                                success: function () {
                                    console.log('insert image success')
                                }
                            })
                            
                        }
                    });
                });
            }
        });
    },
    updateEdit: function (t) {
        html = t.detail.html
    },
    submitData: function () {
        var that = this;
        getApp().core.setStorage({
            key: "goods_detail",
            data: html
        })
        wx.navigateBack({
          delta: 0,
        })
    }
});